<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 * Basic view using native PHP.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel_View
 */
class PHPView extends View
{

  /**
   * (non-PHPdoc)
   * @see simplify2/kernel/view/IView#render($object)
   */
  public function render(IRenderable $object = null)
  {
    if (empty($object))
      $object = $this;
    
    $template = $object->getTemplate();
    
    $output = $this->internalRender($object, $template);
    
    $output = PHPView::filter_lang($output);
    
    $layout = $object->getLayout();
    
    if ($layout !== false) {
      if (empty($layout))
        $layout = $this->defaultLayout;
      
      $view = new PHPView();
      $view->copyAll($object);
      $view->copyAll($this);
      $view->set('layout_content', $output);
      $view->setTemplate($layout);
      $view->setLayout(false);
      $output = $view->render();
    }
    
    return $output;
  }

  /**
   *
   * @param $object
   * @param $template
   * @return unknown_type
   */
  protected function internalRender(IDictionary $object, $template)
  {
    $this->response->header('Content-Type: text/html; charset=UTF-8');
    
    extract($object->getAll(), EXTR_REFS);
    
    ob_start();
    
    require_once ($template);
    
    $output = ob_get_clean();
    
    return $output;
  }

  protected static function filter_lang($output)
  {
    if (preg_match_all('/(<lang( .*?)?>(.*?)(?:<plural\/>(.*?))?<\/lang>)/im', $output, $m)) {
      foreach ($m[0] as $k => $v) {
        $params = array();
        if (preg_match_all('/([a-z]+)="([^"]*)"/im', $m[2][$k], $_params)) {
          foreach ($_params[0] as $_k => $_v) {
            $params[$_params[1][$_k]] = $_params[2][$_k];
          }
        }
        
        $s = $m[3][$k];
        
        $args = array(
          $s 
        );
        
        if (! empty($m[4][$k])) {
          if (! isset($params['count'])) {
            throw new Exception('Missing count attribute in tag: <b>' . htmlentities($m[0][$k]) . '</b>');
          }
          
          $args[] = $m[4][$k];
          $args[] = $params['count'];
          
          if (isset($params['domain'])) {
            $args[] = sy_get_param($params, 'domain');
            $func = '_dn';
          }
          else {
            $func = '_n';
          }
        }
        
        else {
          if (isset($params['domain'])) {
            $args[] = sy_get_param($params, 'domain');
            $func = '_d';
          }
          else {
            $func = '__';
          }
        }
        
        $transl = call_user_func_array($func, $args);
        
        if (isset($params['args'])) {
          $sep = sy_get_param($params, 'argsep', '|');
          $fargs = explode($sep, $params['args']);
          array_unshift($fargs, $transl);
          $transl = call_user_func_array('sprintf', $fargs);
        }
        
        $output = str_replace($m[0][$k], $transl, $output);
      }
    }
    
    return $output;
  }

}

?>